package com.example.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.PageVO;
import com.example.entity.Region;
import com.example.entity.ResultMsg;
import com.example.service.RegionService;
import org.springframework.lang.NonNull;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("/region")
@CrossOrigin(origins = "*")
public class RegionController {

    @Resource
    RegionService regionService;

    @PutMapping("/insertOne")
    public ResultMsg<String> insertOne(@RequestBody Region region) {
        regionService.saveOrUpdate(region);
        return ResultMsg.success();
    }

    @DeleteMapping("/deleteOne")
    public ResultMsg<String> deleteOne(@RequestParam Serializable id) {
        regionService.removeById(id);
        return ResultMsg.success();
    }

    @PostMapping("/updateOne")
    public ResultMsg<String> updateOne(@RequestBody Region region) {
        regionService.saveOrUpdate(region);
        return ResultMsg.success();
    }

    @GetMapping("/getList/{pid}")
    public ResultMsg<List<Region>> getList(@PathVariable("pid") String pid) {
        List<Region> list;
        if (StringUtils.hasText(pid)) {
            list = regionService.queryList(pid);
        } else {
            list = regionService.list();
        }
        return ResultMsg.success(list);
    }

    @PostMapping("/getPage")
    public Page<Region> getPage(@RequestBody @NonNull PageVO<Region> pageVO) {
        return regionService.queryByPage(pageVO);
    }

    @GetMapping("/grabPage/{level}")
    public ResultMsg<Object> grabPage(@PathVariable("level") int level) {
        regionService.grabPage(level);
        return ResultMsg.success();
    }
}